<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/qualitydepartmentemployee/QualityDepartmentEmployeeAction.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/Response.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/Service.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/ServiceFactory.php';

final class MonitoringAction extends QualityDepartmentEmployeeAction {
	protected function execute(Response $response) {
		/* получение типов структурных подразделений 
		 * и активного типа, выбранного пользователем */
		$serviceFactory = new ServiceFactory();
		$service = $serviceFactory->getService('DepartmentTypeService');
		$types = $service->getAll();
		$response->setParameter('types', $types);
		if(isset($_POST['identity'])) {
			$type = $service->read($_POST['identity']);
		} else {
			$type = $types[0];
		}
		$response->setParameter('type', $type);

		/* определение года, для которого выполняется
		 * просмотр целевых показателей */
		if(isset($_POST['year'])) {
			$year = $_POST['year'];
		} elseif (isset($_SESSION['year'])) {
			$year = unserialize($_SESSION['year']);
		} else {
			$year = date("Y");
		}
		$_SESSION['year'] = serialize($year);

		/* получение данных для соответствующих подразделений и календарного года */
		$service = $serviceFactory->getService('DepartmentService');
		$departments = $service->findByType($type);
		$responsibilities = array();
		$indicatorValues = array();
		$service = $serviceFactory->getService('ResponsibilityService');
		$serviceIndicatorV = $serviceFactory->getService('IndicatorValueService');
		foreach($departments as $department) {
			$responsibility = $service->getByDepartmentAndYear($department->getIdentity(), $year);
			$responsibilities[$department->getIdentity()] = array();
			foreach($responsibility as $key => $value){
				if(!$value->isMonitoring()) {
					$responsibilities[$department->getIdentity()][] = $value;
					$indicatorValuesTemp = $serviceIndicatorV->getByResponsibility($value->getIdentity());
					$indicatorValues[$value->getIdentity()] = array();
					foreach ($indicatorValuesTemp as $indicatorValue) {
						$indicatorValues[$value->getIdentity()][$indicatorValue->getQuarter()] = $indicatorValue;
					}
				}
			}
		}
		$response->setParameter('departments', $departments);
		$response->setParameter('responsibilities', $responsibilities);
		$response->setParameter('indicatorValues', $indicatorValues);
		$response->setParameter('title', 'Мониторинг целевых показателей');
	}
}
?>